package cn.foolishbird.crow.springmvc;

import io.swagger.annotations.ApiModel;

/**
 * 统一返回对象
 *
 * @author foolish bird
 */
@ApiModel("json类型返回对象封装")
public class JSONResponse<T> {

    /**
     * 请求成功，默认code
     */
    private static String SUCCESS_CODE = "200";


    /**
     * 请求成功，默认 message
     */
    private static String SUCCESS_MESSAGE = "Success";

    /**
     * 状态码， 默认是200成功
     */
    private String code;

    /**
     * 状态码说明
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 完整的错误说明
     */
    private String errorMessage;

    public JSONResponse() {
    }

    public JSONResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public static void successCode(String successCode) {
        SUCCESS_CODE = successCode;
    }

    public static void successMessage(String successMessage) {
        SUCCESS_MESSAGE = successMessage;
    }

    /**
     * 返回成功
     *
     * @param t 返回数据
     * @return JSONResponse 返回封装对象
     */
    public static <D> JSONResponse<D> ok(D t) {
        JSONResponse<D> response = new JSONResponse<>();
        response.setCode(SUCCESS_CODE);
        response.setMessage(SUCCESS_MESSAGE);
        response.setData(t);
        return response;
    }

    /**
     * 返回成功
     *
     * @return JSONResponse 返回封装对象
     */
    public static <D> JSONResponse<D> ok() {
        return ok(null);
    }

}
